"""
:copyright:
Copyright (C) 2007 Association of Universities for Research in Astronomy (AURA)

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    3. The name of AURA and its representatives may not be used to
      endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AURA ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL AURA BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.


:Author: Christopher Hanley 

:Purpose: 
This application is used to create KMZ files used by GoogleSky
from input FITS or image files.

Possible Input -
1) A single FITS image with valid WCS
2) A single FITS image with valid WCS and registered color image
3) Three FITs images representing 3 color bands.

:Outout:
A kmz file containing all the data necessary for the images to
appear registered in GoogleSky


"""

__docformat__ = 'restructuredtext'

# external dependencies
import pyfits
import numpy
import Image # Python Image Library

# dependencies
import converter
    
class add2sky(object):
    def __init__(self):
        """
        :Purpose: Base object constructor
        """
        self.referenceImage = None
        self.graphic = None
        self.kmz = "add2sky.kmz"
        
        # Options for creating raster images from 3 input FITS files
        self.fitsfile1 = None
        self.fitsfile2 = None
        self.fitsfile3 = None
        
        self.imageOptions = {}
        # Autoconstract algorithm to apply
        self.imageOptions['contrast']='zscale' # Options are zscale and percentile
    
        # Option for scaling pixel values between min and max
        self.imageOptions['scale']='linear' # Options are linear or archsinh

        # Options for autocontrast algorithms
        self.imageOptions['numPoints']=5000 # number of points for subsampling image
        self.imageOptions['numPointsPerRow']=250 # number of points to sample per row 
    
        # Options for zcontrast autoscaling
        self.imageOptions['contrastValue']=0.25

        
    def readSingleFits(self,input):
        # The single Fits file is the referenceImage by definition
        self.referenceImage = input        # need to add input error checking!!!!!!!!!!!!!!!!!!!

    def readThreeFitsForColorImage(self,input1,input2,input3):
        # The first input image will be defined to be the referenceImage
        self.referenceImage = self.fitsfile1 = input1       # need to add input error checking!!!!!!!!!!!!!
        self.fitsfile2 = input2
        self.fitsfile3 = input3
        
    def readColorImage(self,input):
        self.graphic = Image.open(input)
    
    def createColorImage(self):
        self.graphic = converter.multifits2png(self.fitsfile1,self.fitsfile2,self.fitsfile3)

    def setImage(self):
        self.graphic = converter.fits2png(self.referenceImage,self.imageOptions)
        
    def createKMZ(self):
        if self.referenceImage is not None:
            if self.graphic is not None:
                converter.makeKMZ(self.graphic,self.referenceImage,self.kmz)
            else:
                print " Cannot create KMZ file without a graphic file being created first. "
        else:
            print " Cannot create KMZ file without a reference file being defined first. "
                
    def setOutput(self,filename):
        self.kmz=str(filename)
        
    def _verify(self,filename,filetype=None):
        # will have to verify fits files and presence of files on disk
        pass
    
